package com.csx.base.design.behavior.command;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cuisongxu
 * @date 2022/10/12 12:08
 */
public class Keyboard {

    public enum KeyCode {
        F1, F2, ESC, UP, LEFT, RIGHT;
    }

    private Map<KeyCode, List<Command>> keyCommands = new HashMap<>();

    public void bindKeyCommand(KeyCode keyCode, List<Command> commands) {
        this.keyCommands.put(keyCode, commands);
    }

    public void onKeyPressed(KeyCode keyCode) {

        System.out.println(keyCode + "键按下...");
        List<Command> commands = this.keyCommands.get(keyCode);
        if(commands == null) {
            System.out.println("警告: 无效的命令...");
            return;
        }
        // 执行命令
        commands.forEach(Command::exe);
    }
}
